package com.wondersgroup.controller;

import com.wondersgroup.aop.Submit;
import com.wondersgroup.entity.Record;
import com.wondersgroup.entity.RecordVo;
import com.wondersgroup.service.PersonService;
import com.wondersgroup.util.RegUtil;
import com.wondersgroup.util.RsaUtil;
import com.wondersgroup.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/person")
public class PersonController {

  @Autowired
  private PersonService personService;

  /**
   * 访客扫码
   *
   * 先判断 record 表中 是否有数据  再判断 person 表中是否有数据
   */
  @RequestMapping("/visit")
  public Map<String, Object> visit(@RequestBody Map<String, Object> param) {
    Map<String, Object> map = new HashMap<>();
    String openId = param.get("openId") != null ? param.get("openId").toString() : null;
    if (openId.equals("") || openId == null) {
      map.put("code", 500);
      map.put("msg", "参数不能为空!");
      return map;
    }
    //根据openId 获取数据 判断是否注册过
    try {
      map = personService.visit(openId);
    } catch (Exception e) {
      e.printStackTrace();
      map.put("code","500");
      map.put("msg","异常！");
      return map;
    }
    return map;
  }


  /**
   * 登记
   */
  @Submit
  @RequestMapping("/signIn")
  public Map<String, Object> register(@RequestBody Record record) {
    Map<String, Object> map = new HashMap<>();
    if (record.getSjhm() == null || record.getSjhm().equals("") ||
      record.getDwid() == null || record.getDwid().equals("") ||
      record.getOpenId() == null || record.getOpenId().equals("")||
      record.getXm() == null || record.getXm().equals("")) {
      map.put("code", 500);
      map.put("msg", "参数不能为空！");
      return map;
    }
    //解密
    try {
      //姓名 解密 转码
      String xm =RsaUtil.getDecData(record.getXm());
      xm = URLDecoder.decode(xm, "UTF-8" );
      record.setXm(xm);
      //身份证
      record.setSjhm(RsaUtil.getDecData(record.getSjhm()));
      Boolean b = RegUtil.isMobile(record.getSjhm());
      if (b == false) {
        map.put("code", 500);
        map.put("msg", "请输入正确的手机号！");
        return map;
      }
    } catch (Exception e) {
      e.printStackTrace();
      map.put("code", 500);
      map.put("msg", "解密异常！");
      return map;
    }
    //如果传 证件号码 就解密并校验
    if (record.getZjhm() != null && !record.getZjhm().equals("")) {
      try {
        record.setZjhm(RsaUtil.getDecData(record.getZjhm()));
      } catch (Exception e) {
        e.printStackTrace();
      }
      Boolean bo = RegUtil.isIDNumber(record.getZjhm());
      if (bo == false) {
        map.put("code", 500);
        map.put("msg", "请输入正确的证件号码！");
        return map;
      }
    }
    record.setId(UUID.randomUUID().toString().replace("-", ""));
    record.setDjsj(new Date());
    record.setYxbz("1"); //默认有效
    try {
      map = personService.signIn(record);
    } catch (Exception e) {
      e.printStackTrace();
      map.put("code", 500);
      map.put("msg", "登记异常！");
      return map;
    }
    return map;
  }













  /**
   * 重新登记
   */
  @Submit
  @RequestMapping("/getRecord")
  public Map<String, Object> reSignIn(@RequestBody Map<String, Object> param) {
    Map<String, Object> map = new HashMap<>();
    String openId = param.get("openId") != null ? param.get("openId").toString() : null;
    String dwid = param.get("dwid") != null ? param.get("dwid").toString() : null;
    if (openId == null || openId.equals("") || dwid == null || dwid.equals("")) {
      map.put("code", 500);
      map.put("msg", "参数不能为空！");
      return map;
    }
    //根据 dwid openId  获取 最新一次签到记录
    RecordVo vo = null;
    try {
      vo = personService.getLatestRecord(openId, dwid);
    } catch (Exception e) {
      e.printStackTrace();
    }
    if (vo != null) {
      map.put("code", 200);
      map.put("record", vo);
    } else {
      map.put("code", 500);
      map.put("msg", "未注册！");
    }
    return map;
  }


  /**
   * 签退
   */
  @Submit
  @RequestMapping("/signOut")
  public Map<String, Object> signOut(@RequestBody Map<String, Object> param) {
    String openId = param.get("openId") != null ? param.get("openId").toString() : null;
    String dwid = param.get("dwid") != null ? param.get("dwid").toString() : null;
    Map<String, Object> map = new HashMap<>();
    if (openId == null || openId.equals("") || dwid == null || dwid.equals("")) {
      map.put("code", 500);
      map.put("msg", "参数不能为空！");
      return map;
    }
    Date kssj = TimeUtil.getStartTime(new Date());
    Date jssj = TimeUtil.getEndTime(new Date());
    return personService.signOut(openId, dwid, kssj, jssj);
  }


  /**
   * 是否注册
   */
  @RequestMapping("/isRegister")
  public Map<String, Object> isRegister(HttpServletRequest request) {
    String openId = "1234";
    Map<String, Object> map = personService.isRegister(openId, "1");
    return map;
  }


}
